#include <SDL\SDL.h>
#include <SDL\SDL_mixer.h>
#include <SDL\SDL_ttf.h>
#include "GameData.h"
#include "Color.h"
#include <string>
#include <fstream>
#include <iostream>
#include "graphicx.h"
using namespace std;
int main(int argc, char * argv[]) {
	time_t rawtime;
	struct tm timeinfo;

	time ( &rawtime );
	localtime_s(&timeinfo,&rawtime );

#ifdef DEBUG_MODE
	ifstream inp;
	ofstream out;
	string myFileName;

	int i = 1;
	
	do{
		stringstream outStream;
		outStream << i;
		
		myFileName = "RuntimeData\\mydata";
		myFileName += outStream.str();
		myFileName +=".txt";
		inp.open(myFileName.c_str(), ifstream::in);
		inp.close();
		i++;
	}while(!inp.fail());
	inp.clear(ios::failbit);

	out.open(myFileName.c_str(), ofstream::out);
	 char buffer[32];
	out << "Current local time and date:" << asctime_s (buffer,32,&timeinfo) << endl;
#endif

	if(SDL_Init(SDL_INIT_EVERYTHING) != 0) return 1;
	if(TTF_Init() != 0) return 1;
	if(Mix_OpenAudio(44100,MIX_DEFAULT_FORMAT,2,4096) != 0) return 1;
	srand((unsigned int)time(NULL));
	
	SDL_Surface * screen = SDL_SetVideoMode(SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_BPP,/*SDL_FULLSCREEN*/SDL_HWSURFACE | SDL_RESIZABLE);
	
	/* PUT SURFACE DRAWING INTO GAMEDATA/GAMEBOARD */
	SDL_Surface * tileIcons = load_image("Graphics/Tile Art/tiles.bmp");
	SDL_Surface * tileBGs = load_image("Graphics/Tile Art/Tile Backgrounds.bmp");
	SDL_Surface * boardBG = load_image("Graphics/Game Board Background.bmp");
	setTransparentColor(tileIcons, white);
	setTransparentColor(tileBGs, white);

	GameData gd(screen);
	

	while(gd.isRunning()) {
		unsigned int startTimer = SDL_GetTicks();
		gd.update();
		gd.draw();
		gd.handleInput();
		unsigned int endTimer = SDL_GetTicks();
#ifdef DEBUG_MODE
		out << "Time Between iteration of game loop = " << endTimer - startTimer << endl; 
#endif
	}

#ifdef DEBUG_MODE
	out.close();
#endif
	gd.release();
	SDL_FreeSurface(tileBGs);
	SDL_FreeSurface(tileIcons);
	SDL_FreeSurface(screen);
	TTF_Quit();
	Mix_CloseAudio();
	SDL_Quit();

	return 0;
}